from typing import Optional

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMessageBox


class QMessageBoxEx(QMessageBox):
    def __init__(self):
        super().__init__()

    @staticmethod
    def informationEx(title: str, text: str, iconFile: Optional[str] = None):
        msgBox = QMessageBox()
        msgBox.setWindowTitle(title)
        msgBox.setText(text)
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.button(QMessageBox.Ok).setText("确定")
        msgBox.setDefaultButton(QMessageBox.Ok)
        if iconFile is not None:
            msgBox.setWindowIcon(QIcon(iconFile))
        return msgBox.exec_()

    @staticmethod
    def questionEx(title: str, text: str, iconFile: Optional[str] = None):
        msgBox = QMessageBox()
        msgBox.setWindowTitle(title)
        msgBox.setText(text)
        msgBox.setIcon(QMessageBox.Question)
        msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msgBox.button(QMessageBox.Yes).setText("退出")
        msgBox.button(QMessageBox.No).setText("取消")
        msgBox.setDefaultButton(QMessageBox.No)
        if iconFile is not None:
            msgBox.setWindowIcon(QIcon(iconFile))
        return msgBox.exec_()

    @staticmethod
    def warningEx(title: str, text: str, iconFile: Optional[str] = None):
        msgBox = QMessageBox()
        msgBox.setWindowTitle(title)
        msgBox.setText(text)
        msgBox.setIcon(QMessageBox.Warning)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.button(QMessageBox.Ok).setText("确定")
        msgBox.setDefaultButton(QMessageBox.Ok)
        if iconFile is not None:
            msgBox.setWindowIcon(QIcon(iconFile))
        return msgBox.exec_()

    @staticmethod
    def criticalEx(title: str, text: str, iconFile: Optional[str] = None):
        msgBox = QMessageBox()
        msgBox.setWindowTitle(title)
        msgBox.setText(text)
        msgBox.setIcon(QMessageBox.Critical)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.button(QMessageBox.Ok).setText("确定")
        msgBox.setDefaultButton(QMessageBox.Ok)
        if iconFile is not None:
            msgBox.setWindowIcon(QIcon(iconFile))
        return msgBox.exec_()


